package com.jj.core.utils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by luhan
 * 2018/1/2, 13:59
 */
public class StrUtil {
    private static Logger logger = LoggerFactory.getLogger(StrUtil.class);
    /**
     * 计算字符串长度，unicode双字节字符算2，asci字符算1
     * @param str
     * @return
     */
    public static int calcStrLen(String str){
        int total = 0;
        if(!StringUtils.isEmpty(str)){
            for(int i = 0; i < str.length() ;i++){
                int n = str.charAt(i);
                if(n > 0x7F){
                    total = total + 2;
                }else{
                    total = total + 1;
                }
            }
        }
        return total;
    }

    public static void main(String[] args) {
        String name = "中a国b人1";
        logger.debug("{}", StrUtil.calcStrLen(name));
    }
}
